function varargout = TLPreLookupDlg(action, varargin)

% TL_PRELOOKUP_DLG manages the user interface for TargetLink Prelook-Up block
%
% SYNTAX dlgInfo = TLPrelookupDlg('GetDlgInfo');
%   gets dialog info of TargetLink Constant block
%
%   INPUT ARGUMENTS
%   -/-
%
%   OUTPUT ARGUMENTS
%   dlgInfo     dialog information contains pageNames etc.
%
% SYNTAX [sheetDims, ctrlPos] = TLPrelookupDlg('CtrlPositions', pageName, commonGeom, dlgInfo, sheetDims);
%   calculates the control positions of all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   commonGeom      common geometry data
%   dlgInfo         dialog information contains pageNames etc.
%   sheetDims       dimension of sheet
%
%   OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% SYNTAX dlgdata = TLPrelookupDlg('Create', pageName, dlgfig, dlgdata);
%   creates all controls of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX [dlgdata,bModified] = TLPrelookupDlg('Manage', pageName, dlgfig, dlgdata, pageAction);
%   manages all actions of dialog page: <pageName>
%
%   INPUT ARGUMENTS
%   pageName        name of current page
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageAction      modified control of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%   bModified       flag = 1, if dialog data are modified, =0 otherwise
%
% SYNTAX dlgdata = TLPrelookupDlg('Update', dlgfig, dlgdata, pageNum);
%   updates all controls of dialog page: <pageNum>
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   dlgdata         dialog data
%   pageNum         number of current page
%
%   OUTPUT ARGUMENTS
%   dlgdata         modified dialog data
%
% SYNTAX TLPrelookupDlg('Apply', dlgfig, dlgdata);
%   writes current blockdata to block and DD
%
%   INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit, T. Pietzsch
% $RCSfile: TLPrelookupDlg.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_SimBlocksUI/Sources/MSrc/project.pj $
% $Revision: 1.28 $
% $Date: 2011/09/02 09:55:40MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.


switch action
    case 'GetDlgInfo'
        hBlock = varargin{1};
        dlgInfo = tl_get_dlginfodefaults('tlsim');
        titleName = 'Prelookup';
        pathinfo = tl_pathinfo(hBlock);
        dlgInfo.title = sprintf('TargetLink %s: %s', titleName, pathinfo.shortDisplayName);
        dlgInfo.pageNames   = {
            'General';
            'Axis';
            'Index';
            'Fraction'
            'Logging & Autoscaling';
            'Documentation'};
        varargout{1} = dlgInfo;
        varargout{2} = get_tlcg_data(hBlock);
        
    case 'CtrlPositions'
        pageName     = varargin{1};
        commonGeom   = varargin{2};
        sheetDims    = varargin{4};
        excludedCtrls = {'saturate', 'inheritproperties', 'scalarExp'};
        switch pageName
            case 'General'
                [sheetDims, ctrlPos] = FcnCalcTableCtrlPositions(commonGeom);
                
            case 'Axis'
                [sheetDims, ctrlPos] = FcnCalcBreakpointDataCtrlPositions(commonGeom);
                
            case 'Index'
                [sheetDims, ctrlPos] = tl_calc_ctrls_positions(commonGeom, excludedCtrls);
                
            case 'Fraction'
                [sheetDims, ctrlPos] = tl_calc_ctrls_positions(commonGeom, excludedCtrls);
                
                
            case 'Logging & Autoscaling'
                [sheetDims, ctrlPos] = FcnCalcLoggingCtrlPositions(commonGeom, sheetDims);
                
            case 'Documentation'
                % block has extra table controls
                bTblBlock = 1;
                bCodeComment = 1;
                [sheetDims, ctrlPos] = ...
                    TLManageDocCtrls('ctrlpos', commonGeom, sheetDims, ...
                    bCodeComment, bTblBlock);
        end
        varargout{1} = sheetDims;
        varargout{2} = ctrlPos;
        
    case 'Create'
        pageName = varargin{1};
        dlgfig   = varargin{2};
        dlgdata  = varargin{3};
        pageNum  = strmatch(pageName, dlgdata.pageNames, 'exact');
        switch pageName
            case 'General'
                dlgdata = FcnCreateTableCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Axis'
                dlgdata = FcnCreateBreakpointDataCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Index'
                dlgdata = TLManageVariableCtrls('create', pageNum, dlgfig, dlgdata);
                dlgdata = TLManageScalingCtrls('create', pageNum, dlgfig, dlgdata);
                
            case 'Fraction'
                dlgdata = TLManageVariableCtrls('create', pageNum, dlgfig, dlgdata);
                dlgdata = TLManageScalingCtrls('create', pageNum, dlgfig, dlgdata);
                
            case 'Logging & Autoscaling'
                dlgdata = FcnCreateLoggingCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Documentation'
                dlgdata = FcnCreateDocumentationCtrls(pageNum, dlgfig, dlgdata);
        end
        dlgdata.pageData{pageNum}.ctrlPos = [];
        varargout{1} = dlgdata;
        
    case 'Manage',
        pageName   = varargin{1};
        dlgfig     = varargin{2};
        dlgdata    = varargin{3};
        pageAction = varargin{4};
        pageNum    = strmatch(pageName, dlgdata.pageNames, 'exact');
        switch pageName
            case 'General'
                [dlgdata, bModified] = FcnManageTableCtrls(pageNum, dlgdata, pageAction);
                
            case 'Axis'
                [dlgdata, bModified] = FcnManageBreakpointDataCtrls(pageNum, dlgfig, dlgdata, pageAction);
                
            case 'Index'
                [dlgdata, bModified1] = TLManageVariableCtrls('manage', pageNum, dlgfig, dlgdata, pageAction, 'index');
                [dlgdata, bModified2] = TLManageScalingCtrls('manage', pageNum, dlgfig, dlgdata, pageAction, 'index', 1);
                
                bModified = bModified1 || bModified2;
                
            case 'Fraction'
                [dlgdata, bModified1] = TLManageVariableCtrls('manage', pageNum, dlgfig, dlgdata, pageAction, 'fraction');
                [dlgdata, bModified2] = TLManageScalingCtrls('manage', pageNum, dlgfig, dlgdata, pageAction, 'fraction', 1);
                
                bModified = bModified1 || bModified2;
                
            case 'Logging & Autoscaling'
                [dlgdata, bModified] = FcnManageLoggingCtrls(pageNum, dlgdata, pageAction);
                
            case 'Documentation'
                [dlgdata, bModified] = FcnManageDocumentationCtrls(pageNum, ...
                    dlgfig, dlgdata, pageAction);
                
        end
        varargout{1} = dlgdata;
        varargout{2} = bModified ;
        
    case 'ManageSystemIcons',
        dlgfig     = varargin{1};
        dlgdata    = varargin{2};
        pageAction = varargin{3};
        [dlgdata, bModified] = FcnManageSystemIcons(dlgfig,dlgdata,pageAction);
        varargout{1} = dlgdata;
        varargout{2} = bModified;
        
    case 'Update'
        dlgfig    = varargin{1};
        dlgdata   = varargin{2};
        pageNum   = varargin{3};
        
        pageName  = dlgdata.pageNames{pageNum};
        switch pageName
            case 'General'
                dlgdata = FcnUpdateTableCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Axis'
                dlgdata = FcnUpdateBreakpointDataCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Index'
                % signal name of simulated ranges is needed
                dlgdata.signalName = 'k';
                
                dlgdata = TLManageVariableCtrls('update', pageNum, dlgdata, 'index');
                dlgdata = TLManageScalingCtrls('update', pageNum, dlgdata, 'index', []);
                
                TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
                TLManageScalingCtrls('setCtrls', pageNum, dlgfig, dlgdata);
                
            case 'Fraction'
                dlgdata = FcnUpdateFractionCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Logging & Autoscaling'
                dlgdata = FcnUpdateLoggingCtrls(pageNum, dlgfig, dlgdata);
                
            case 'Documentation'
                dlgdata = FcnUpdateDocumentationCtrls(pageNum, dlgfig, dlgdata);
        end
        varargout{1} = dlgdata;
        
    case 'Apply'
        dlgdata  = varargin{2};
        bClose   = varargin{3};
        [dlgdata, bClose] = tl_apply_blockdata(dlgdata, bClose);
        varargout{1} = dlgdata;
        varargout{2} = bClose;
        
    otherwise
        error(['Invalid action "' action '"']);
        
end


%-----------------------------------------------------------------------------------------
% FcnManageSystemIcons manage actions of toolbar icons
%
% INPUT ARGUMENTS
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   action      action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageSystemIcons(dlgfig, dlgdata, action) %#ok

bModified = false;
switch action
    case 'EditCode'
        % look for associated production code file
        [codefile,blockstr] = get_codefile(dlgdata.model);
        if ~isempty(codefile)
            codeedit(codefile,blockstr);
        end
        
    case 'ViewCode'
        switch dlgdata.pageNames{dlgdata.CurrentPageNum}
            case 'Axis'
                blockVar = 'input';
                tl_create_links_to_model('ShowDeclaration',dlgdata.model, blockVar);
            case 'Fraction'
                blockVar = 'fraction';
                tl_create_links_to_model('ShowComputation',dlgdata.model, blockVar);
            otherwise
                blockVar = 'index';
                tl_create_links_to_model('ShowComputation',dlgdata.model, blockVar);
        end
        
    otherwise
        error('Invalid callback');
        
end % switch(action)


%-----------------------------------------------------------------------------------------
% FcnCalcTableCtrlPositions calculates positions for all controls on General page
%
% INPUT ARGUMENTS
%   commonGeom      common geometry information
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% HINT:
% Positions are calculated from the bottom left of the figure to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCalcTableCtrlPositions(commonGeom)

% get sheet dimension of output tab
[sheetDims, outputCtrlPos, col, colDelta, rowDelta] = tl_calc_ctrls_positions(commonGeom, ...
    {'allscaling', 'scaling', 'type', 'class', 'var', 'element', 'width', 'address' 'name', 'unit', 'scalarExp', 'inheritproperties'});

strings_and_styles = {...
    'Data Dictionary lookup table object:', 'text';...
    'n',                                    'pushbutton';...
    'Axis (Breakpoint data):',              'text';
    repmat('m', 74),                       'edit';
    'Output only the index',                'checkbox';
    ' Scale Vector ',                       'pushbutton'};

[ddLabelSize, ...
    ddMenuButtonSize, ...
    tableLabelSize, ...
    tableEditSize, ...
    outputmodeCheckboxSize, ...
    autoscaleButtonSize] = ...
    ds_get_uicontrol_sizes(commonGeom,strings_and_styles);

% correct height, because on some platforms icon overlapped button boundary; icon height is 18 pixel.
ddMenuButtonSize(2) = max(22, ddMenuButtonSize(2));

% correct length of table edit field
tableEditSize(1) = outputCtrlPos.tlcgoptGroupbox(3) - 2*commonGeom.frameSideEdgeBuffer;
cx = commonGeom.figSideEdgeBuffer + commonGeom.sheetSideEdgeBuffer;
cy = commonGeom.bottomSheetOffset + ...
    commonGeom.sheetBottomEdgeBuffer;

tableGroupboxWidth = outputCtrlPos.addoptGroupbox(3);
tableGroupboxHeight = 5 * commonGeom.editHeight ...
    + 2 * commonGeom.textHeight ...
    + 6 * rowDelta ...
    + commonGeom.frameBottomEdgeBuffer ...
    + commonGeom.frameTopEdgeBuffer;
ctrlPos.tableGroupbox = [cx, cy, tableGroupboxWidth, tableGroupboxHeight];

% positions for table controls
% autoscale button
cx = col(1);
cy = commonGeom.frameBottomEdgeBuffer;
ctrlPos.autoscaleButton = [cx cy 2*autoscaleButtonSize(1) autoscaleButtonSize(2)];

% search algorithm popup
cy = cy + commonGeom.editHeight + rowDelta;
ctrlPos.searchPopup = [cx cy ctrlPos.autoscaleButton(3:4)];

% output checkbox
cy = cy + commonGeom.editHeight + rowDelta;
ctrlPos.outputmodeCheckbox = [cx cy outputmodeCheckboxSize];

% table edit field
cy = cy + commonGeom.editHeight + rowDelta;
cx = col(1);
ctrlPos.uEdit = [cx cy tableEditSize];

% table label
cy = cy + commonGeom.textHeight + rowDelta;
ctrlPos.uLabel = [cx cy tableLabelSize];

% 2nd row
cy = cy + commonGeom.editHeight + rowDelta;

% DD field
cx = col(1);
dotButtonSize = [2*ddMenuButtonSize(1) ddMenuButtonSize(2)];
ddEditSize = [tableEditSize(1) - dotButtonSize(1), commonGeom.editHeight];
ctrlPos.ddEdit = [cx cy ddEditSize];

% select button
cx = cx + ddEditSize(1);
ctrlPos.ddSelectButton = [cx,cy, dotButtonSize];
% DD menu button
cx = cx + ddMenuButtonSize(1);
ctrlPos.ddMenuButton = [cx+2,cy+2,ddMenuButtonSize(1)-4 ddMenuButtonSize(2)-4];


% 1st row
cy = cy + commonGeom.textHeight + rowDelta;
% DD label
cx = col(1);
ctrlPos.ddLabel = [cx cy ddLabelSize];

sheetDims(2) = ...
    + ctrlPos.tableGroupbox(4) ...
    + commonGeom.textHeight;


%-----------------------------------------------------------------------------------------
% FcnCreateTableCtrls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateTableCtrls(pageNum, dlgfig, dlgdata)

Children = dlgdata.pageData{pageNum}.children;
ctrlPos = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};

defColor = get(0, 'DefaultUIControlBackgroundColor');
userDataMenuButton.cDataEnabled  = tl_get_icon('ArrowHeadDown_10x18', 'BackgroundColor', defColor);
userDataMenuButton.cDataDisabled = tl_get_icon('ArrowHeadDown_10x18_disabled');
userDataSelectButton.cDataEnabled  = tl_get_icon('Browse_Seperator_24x18', 'BackgroundColor', defColor);
userDataSelectButton.cDataDisabled = tl_get_icon('Browse_Seperator_24x18');

% create the tableGroupbox which will contain table options
Children.tableGroupbox = uipanel( ...
    'Parent',            dlgfig, ...
    'Title',             {'Look-up options'}, ...
    'Units',             'pixels', ...
    'Position',          ctrlPos.tableGroupbox);

% dd controls
Children.ddLabel = uicontrol( ...
    'Parent',            Children.tableGroupbox, ...
    'Style',             'text', ...
    'String',            'Data Dictionary look-up table object:', ...
    'Position',          ctrlPos.ddLabel);

Children.ddEdit = uicontrol( ...
    'Parent',            Children.tableGroupbox, ...
    'BackgroundColor',   'w', ...
    'Style',             'edit', ...
    'Position',          ctrlPos.ddEdit, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''ddEdit'')',pageName));

Children.ddSelectButton = uicontrol( ...
    'Parent',            Children.tableGroupbox, ...
    'Style',             'pushbutton', ...
    'UserData',          userDataSelectButton, ...
    'CData',             userDataSelectButton.cDataEnabled, ...
    'Position',          ctrlPos.ddSelectButton, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''ddSelectButton'')',pageName));

Children.ddMenuButton = uicontrol( ...
    'Parent',            Children.tableGroupbox, ...
    'Style',             'pushbutton', ...
    'UserData',          userDataMenuButton, ...
    'CData',             userDataMenuButton.cDataEnabled, ...
    'Position',          ctrlPos.ddMenuButton, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''ddMenuButton'')',pageName));

% define context menu for DataDictionary browse button
pdlgfig = get(dlgfig,'Parent');
ddCMenu = uicontextmenu('Parent', pdlgfig);
set(Children.ddEdit, 'UIContextMenu', ddCMenu);
set(Children.ddMenuButton, 'UIContextMenu', ddCMenu);
menu.select = uimenu(ddCMenu, ...
    'Label', 'Select', ...
    'Callback', sprintf('tldlg(''%s'',gcbf,''ddSelectButton'')', dlgdata.pageNames{pageNum}));
menu.show = uimenu(ddCMenu, ...
    'Label', 'Show in DD Manager', ...
    'Callback', sprintf('tldlg(''%s'',gcbf,''ddShowDDObject'')', dlgdata.pageNames{pageNum}), ...
    'Separator', 'on');
set(ddCMenu, 'UserData', menu);

% table  controls
Children.uLabel = uicontrol( ...
    'Parent',             Children.tableGroupbox, ...
    'Style',              'text', ...
    'String',             'Axis (breakpoint data):', ...
    'Position',           ctrlPos.uLabel);

Children.uEdit = uicontrol( ...
    'Parent',             Children.tableGroupbox, ...
    'BackgroundColor',    'w', ...
    'Style',              'edit', ...
    'Position',           ctrlPos.uEdit, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''uEdit'')',pageName));

Children.outputmodeCheckbox = uicontrol( ...
    'Parent',             Children.tableGroupbox, ...
    'Style',              'checkbox', ...
    'String',             'Output only the index', ...
    'Position',           ctrlPos.outputmodeCheckbox, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''outputmodeCheckbox'')',pageName));

Children.searchPopup = uicontrol( ...
    'Parent',             Children.tableGroupbox, ...
    'Style',              'popupmenu', ...
    'BackgroundColor',    'w', ...
    'String',             {'Linear search, start low'; ...
    'Linear search, start high'; ...
    'Local search'; ...
    'Binary search'}, ...
    'Position',           ctrlPos.searchPopup, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''searchPopup'')',pageName));

% autoscale button
Children.autoscaleButton = uicontrol( ...
    'Parent',             Children.tableGroupbox, ...
    'Style',              'pushbutton', ...
    'String',             'Scale Vector', ...
    'Position',           ctrlPos.autoscaleButton, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''autoscaleButton'')',pageName));

dlgdata.pageData{pageNum}.children  = Children;

%-----------------------------------------------------------------------------------------
% FcnManageTableCtrls manages control actions of table page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageTableCtrls(pageNum, dlgdata, pageAction)

bModified = false;
bResetDdvFlag = false;
Children = dlgdata.pageData{pageNum}.children;
panelPos = get(Children.tableGroupbox, 'Position');

switch pageAction
    
    case 'ddEdit'
        % Edit field: Get new module from edit field
        dlgdata.data.blockobject = tl_deblank(get(Children.ddEdit,'String'),2);
        bModified = true;
        bResetDdvFlag = true;
        
    case 'ddMenuButton',
        FcnSetMenuPosition(Children.ddMenuButton, panelPos(1:2));
        bModified = true;
        
    case 'ddSelectButton'
        dlgdata.data.blockobject = dsdd( 'SelectObject', ...
            'entryPath',       '/Pool/BlockSpecifications', ...
            'CurrentPath',     dlgdata.data.blockobject, ...
            'ObjectKind',      'Block');
        bModified = true;
        bResetDdvFlag = true;
        
    case 'ddShowDDObject'
        if dsdd('exist', ['/Pool/BlockSpecifications/' dlgdata.data.blockobject]);
            % jump to function object in DD Manager
            ddPath = ['/Pool/BlockSpecifications/' dlgdata.data.blockobject];
            dsddman('Select', ddPath);
        else
            % jump to node in DD Manager
            dsddman('Select', '/Pool');
        end
        
    case 'uEdit'
        uStr = deblank(get(Children.uEdit, 'String'));
        if ~isempty(uStr)
            if strcmp(uStr, 'ddv')
                if isempty(dlgdata.data.input.variable)
                    beep;
                else
                    dlgdata.data.input.value = ['ddv(''', dlgdata.data.input.variable, ''')'];
                    dlgdata.data.input.useddvariablevalue = true;
                end
            else
                dlgdata.data.input.value = uStr;
            end
        else
            beep;
        end
        bModified = true;
        
    case 'autoscaleButton'
        ds_error_none;
        [dlgdata.data, bModified, msgstruct] = tl_scale_parameters( ...
            dlgdata.model, dlgdata.data);
        ds_error_register(msgstruct);
        ds_msgdlg('Update', ...
            'Title', ['Scaling of PreLookup Index Search Block ' get_tlsubsystempath(dlgdata.model) ], ...
            'ShowErrors', 'yes', ...
            'ShowNotes', 'yes', ...
            'ShowWarnings', 'yes');
        
    case 'searchPopup'
        dlgdata.data.input.search = get(Children.searchPopup, 'Value');
        bModified = true;
        
    case 'outputmodeCheckbox'
        dlgdata.data.outputonlytheindex = get(Children.outputmodeCheckbox, 'Value');
        bModified = true;
end

if bResetDdvFlag
    dlgdata.data.input.useddvariablevalue = false;
end


%-------------------------------------------------------------------------------
% FcnSetMenuPosition calculates position of contextmenu
%
% INPUT ARGUMENTS
%   ctrlHandle  handle of control
%   panelStart  cx position of panel
%
% OUTPUT ARGUMENTS
%   - / -
%-------------------------------------------------------------------------------
function FcnSetMenuPosition(ctrlHandle, panelStart)

pos = get(ctrlHandle, 'Position') + [panelStart 0 0];
cmenu = get(ctrlHandle, 'UIContextmenu');
commonGeom = ds_get_common_geometry;
textExtent = commonGeom.textExtent;
labelStrings = get(get(cmenu, 'Children'), 'Label');
maxext = 0;
for k = 1 : numel(labelStrings)
    set(textExtent, 'String', labelStrings{k});
    ext = get(textExtent, 'Extent');
    maxext = max(maxext, ext(3));
end
position = [pos(1)-32-maxext, pos(2)];
set(cmenu, 'Position', position, 'Visible', 'on');


%--------------------------------------------------------------------------
% FcnUpdateTableCtrls updates the controls of the table tab
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number (=1 for table page)
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateTableCtrls(pageNum, dlgfig, dlgdata)

Children = dlgdata.pageData{pageNum}.children;
data = dlgdata.data;
bIsFullVs = dlgdata.bIsFullVs;
if bIsFullVs && dlgdata.bDSDDMan
    defaultStatusDD = 'on';
else
    defaultStatusDD = 'inactive';
end

[data, extdata] = tl_sync_dd_blockdata(data, 'BlockType', dlgdata.maskType);

ddEditCtrlStatus = struct('enable', defaultStatusDD);
ddEditCtrlStatus.string = data.blockobject;
ddEditCtrlStatus.iserror = 0;

ddSelectButtonCtrlStatus = struct('enable', defaultStatusDD); %#ok<NASGU>
ddMenuButtonCtrlStatus = struct('enable', defaultStatusDD); %#ok<NASGU>
% set status of context menu controls:
ddCMenu = get(Children.ddMenuButton, 'UIContextMenu');
ddMenu = get(ddCMenu, 'UserData');

menuSelectStatus = struct('enable', flag2str(bIsFullVs));
UserData = get(ddMenu.select, 'UserData');
UserData.ctrlStatus = menuSelectStatus;
set(ddMenu.select, 'UserData', UserData);

menuShowStatus = struct('enable', flag2str(dlgdata.bDSDDMan));
UserData = get(ddMenu.show, 'UserData');
UserData.ctrlStatus = menuShowStatus;
set(ddMenu.show, 'UserData', UserData);

% values and look-up method is independend of blockset mode
uEditCtrlStatus = struct('enable', FcnbEnabled(extdata.input.bWritable.value, 1));
outputmodeCheckboxCtrlStatus = struct('enable', FcnbEnabled(extdata.bWritable.outputonlytheindex, bIsFullVs), ...
    'value', 0);
searchPopupCtrlStatus = struct('enable', FcnbEnabled(extdata.input.bWritable.search, bIsFullVs), ...
    'string', {get(Children.searchPopup, 'String')}, ...
    'value', 1);

% get minima and maxima of breakpoint data
[u, umin, umax, bCompliant] = tl_eval_table(data.input, true);

% adjust some of the standard controls
uEditCtrlStatus.iserror = ~bCompliant;

outputmodeCheckboxCtrlStatus.value = data.outputonlytheindex;
searchPopupCtrlStatus.value = data.input.search;
uEditCtrlStatus.string = data.input.value;

% initialize scale button status
autoscaleButtonCtrlStatus = struct('enable', 'on');
% activation of 'Scale Table' button
% special behaviour of table blocks: Scale button must be disabled if 'no
% autoscaling' is selected
bOutputInteger = ds_isa(['/Pool/Typedefs/' data.index.type], 'integertype');
bAutoscEnable = bOutputInteger ...
    && isempty(dsdd('GetConstraintsScalingRef', ['/Pool/Typedefs/' data.index.type])) ...
    && isempty(data.index.scaling);
bScaleButtonEnable  = dlgdata.bIsFullVs && bAutoscEnable && data.autoscaling.mode == 1;
bIsGenericTable = isempty(data.blockobject);
autoscaleButtonCtrlStatus.enable = flag2str(bScaleButtonEnable && bIsGenericTable);

% set control status
ctrlsList = {
    'ddEdit' ...
    'ddSelectButton' ...
    'ddMenuButton' ...
    'autoscaleButton' ...
    'uEdit' ...
    'outputmodeCheckbox' ...
    'searchPopup'};
% write back control status of controls to the controls' UserData property
for ctrlsNo = 1:length(ctrlsList)
    ctrl = Children.(ctrlsList{ctrlsNo});
    UserData = get(ctrl, 'UserData');
    UserData.ctrlStatus = eval([ctrlsList{ctrlsNo} 'CtrlStatus']);
    set(ctrl, 'UserData', UserData);
end

% set control status of  table parameter groupbox controls
TLSetCtrlsStatus(dlgfig, Children, ctrlsList);


%-----------------------------------------------------------------------------------------
% FcnCalcBreakpointDataControlPositions calculates positions for all controls on breakpoint data page
%
% INPUT ARGUMENTS
%   commonGeom      common geometry information
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%
% HINT:
% Positions are calculated from the bottom left of the figure to avoid
% dependencies on figure size
%-----------------------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCalcBreakpointDataCtrlPositions(commonGeom)

excludedCtrls = {'saturate', 'inheritproperties', 'width', 'element', 'scalarExp'};

[sheetDims, ctrlPos, col, colDelta, rowDelta] = ...
    tl_calc_ctrls_positions(commonGeom, excludedCtrls);

strings_and_styles = { ...
    'Axis (breakpoint data):', 'text'
    'Use value of Data Dictionary variable', 'checkbox'};

[tableLabelSize, useDDValueCheckboxSize] = ds_get_uicontrol_sizes(commonGeom, strings_and_styles);

% control positions of controls of table options group box
% define new height for addoptGroupbox
ctrlPos.addoptGroupbox(4) = commonGeom.editHeight ...
    + commonGeom.checkboxHeight ...
    + rowDelta ...
    + commonGeom.frameBottomEdgeBuffer ...
    + commonGeom.frameTopEdgeBuffer;

%---------------------------------------
% 2nd row
cy = commonGeom.frameBottomEdgeBuffer;

% ddv checkbox
cx = col(1) + tableLabelSize(1) + colDelta / 3;
ctrlPos.useDDValueCheckbox = [cx, cy, useDDValueCheckboxSize];

%---------------------------------------
% 1st row
cy = cy + commonGeom.checkboxHeight + rowDelta;

% table options label
cx = col(1);
ctrlPos.tableLabel  = [cx cy tableLabelSize];

% table options edit field
cx = cx + tableLabelSize(1) + colDelta / 3;
dx = ctrlPos.descEdit(3) - (cx - ctrlPos.descEdit(1));
ctrlPos.tableEdit  = [cx cy dx  ctrlPos.descEdit(4)];

% increase sheet dimensions
sheetDims(2) = sheetDims(2) ...
    + ctrlPos.addoptGroupbox(4) ...
    + commonGeom.textHeight;


%-----------------------------------------------------------------------------------------
% FcnCreateBreakpointDataCtrls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateBreakpointDataCtrls(pageNum, dlgfig, dlgdata)

% invoke standard functions to create variable and scaling controls
dlgdata = TLManageVariableCtrls('create', pageNum, dlgfig, dlgdata);
dlgdata = TLManageScalingCtrls('create', pageNum, dlgfig, dlgdata);

Children = dlgdata.pageData{pageNum}.children;
ctrlPos = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};

% create the addoptGroupbox which will contain table values
Children.addoptGroupbox = uipanel( ...
    'Parent',            dlgfig, ...
    'Title',            {'Axis values'}, ...
    'Units',            'pixels', ...
    'Position',          ctrlPos.addoptGroupbox);

% table  controls
Children.uLabel = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'text', ...
    'String',             'Axis (breakpoint data):', ...
    'Position',           ctrlPos.tableLabel);

Children.uEdit = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'BackgroundColor',    'w', ...
    'Style',              'edit', ...
    'Position',           ctrlPos.tableEdit, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''uEdit'')',pageName));

Children.useDDValueCheckbox = uicontrol( ...
    'Parent',             Children.addoptGroupbox, ...
    'Style',              'checkbox', ...
    'String',             'Use value of Data Dictionary variable', ...
    'Position',           ctrlPos.useDDValueCheckbox, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''useDDValueCheckbox'')',pageName));

dlgdata.pageData{pageNum}.children = Children;


%-----------------------------------------------------------------------------------------
% FcnManageBreakpointDataCtrls manage control actions of breakpoint data page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageBreakpointDataCtrls(pageNum, dlgfig, dlgdata, action)

Children = dlgdata.pageData{pageNum}.children;

switch action
    case 'uEdit'
        uStr = deblank(get(Children.uEdit, 'String'));
        if ~isempty(uStr)
            if strcmp(uStr, 'ddv')
                if isempty(dlgdata.data.input.variable)
                    beep;
                else
                    dlgdata.data.input.value = ['ddv(''', dlgdata.data.input.variable, ''')'];
                    dlgdata.data.input.useddvariablevalue = true;
                end
            else
                dlgdata.data.input.value = uStr;
            end
        else
            beep;
        end
        bModified = true;
        
    case 'useDDValueCheckbox'
        if get(Children.useDDValueCheckbox, 'Value')
            dlgdata.data.input.value = ['ddv(''', dlgdata.data.input.variable, ''')'];
            dlgdata.data.input.useddvariablevalue = true;
        else
            dlgdata.data.input.useddvariablevalue = false;
        end
        bModified = true;
        
    otherwise
        [dlgdata, bModified1] = TLManageVariableCtrls('manage', ...
            pageNum, dlgfig, dlgdata, action, 'input');
        [dlgdata, bModified2] = TLManageScalingCtrls('manage', ...
            pageNum, dlgfig, dlgdata, action, 'input', 1);
        bModified = bModified1 | bModified2;
        
end


%--------------------------------------------------------------------------
% FcnUpdateBreakpointDataCtrls updates the controls of the table tab
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateBreakpointDataCtrls(pageNum, dlgfig, dlgdata)

Children = dlgdata.pageData{pageNum}.children;

% invoke standard update methods
dlgdata = TLManageVariableCtrls('update', pageNum, dlgdata, 'input');
[u, umin, umax, bCompliant] = tl_eval_table(dlgdata.data.input, true);
constraint = [];
if bCompliant
    constraint = [umin umax];
end
dlgdata = TLManageScalingCtrls('update', pageNum, dlgdata, 'input', constraint);

% table value cannot be edited if a look-up table object specifies all
% block data
bWritable = dlgdata.pageData{pageNum}.bWritable.value && ~dlgdata.data.input.useddvariablevalue;
uEditCtrlStatus = struct('enable', FcnbEnabled(bWritable, 1));
uEditCtrlStatus.string = dlgdata.data.input.value;
uEditCtrlStatus.iserror = ~bCompliant;

% status of ddv checkbox
bWritable = dlgdata.pageData{pageNum}.bWritable.value && ~isempty(dlgdata.data.input.variable);
useDDValueCheckboxCtrlStatus.enable = flag2str(bWritable);
useDDValueCheckboxCtrlStatus.value = dlgdata.data.input.useddvariablevalue;

% read ranges' CtrlStatus
simulatedLabelCtrlStatus      = tl_get_control_userdata(Children.simulatedLabel);
upperSimulatedValueCtrlStatus = tl_get_control_userdata(Children.upperSimulatedValue);
lowerSimulatedValueCtrlStatus = tl_get_control_userdata(Children.lowerSimulatedValue);

if ~isempty(u) && bCompliant
    minstr = sprintf('%0.5g', umin);
    maxstr = sprintf('%0.5g', umax);
else
    minstr = 'n.a.';
    maxstr = 'n.a.';
end

simulatedLabelCtrlStatus.string = 'of vector';
upperSimulatedValueCtrlStatus.string = maxstr;
lowerSimulatedValueCtrlStatus.string = minstr;

ctrlsList = {
    'uEdit' ...
    'simulatedLabel' ...
    'upperSimulatedValue' ...
    'lowerSimulatedValue' ...
    'useDDValueCheckbox' ...
    };
for ctrlsNo = 1:numel(ctrlsList)
    ctrl = Children.(ctrlsList{ctrlsNo});
    UserData = get(ctrl, 'UserData');
    UserData.ctrlStatus = eval([ctrlsList{ctrlsNo} 'CtrlStatus']);
    set(ctrl, 'UserData', UserData);
end

% set control status of variable, scaling and table parameter groupbox controls
TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
TLManageScalingCtrls('setCtrls', pageNum, dlgfig, dlgdata);
TLSetCtrlsStatus(dlgfig, Children, {'uEdit', 'useDDValueCheckbox'});


%--------------------------------------------------------------------------
% FcnUpdateFractionCtrls updates the controls of the index tab
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number (=1 for table page)
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateFractionCtrls(pageNum, dlgfig, dlgdata)

Children = dlgdata.pageData{pageNum}.children;

% signal name of simulated ranges is needed
dlgdata.signalName = 'f';

dlgdata = TLManageVariableCtrls('update', pageNum, dlgdata, 'fraction');
dlgdata = TLManageScalingCtrls('update', pageNum, dlgdata, 'fraction', []);

if dlgdata.data.outputonlytheindex
    % disable all controls
    ctrlNames = fieldnames(Children);
    userData.ctrlStatus.enable = 'off';
    structfun(@(c) (set(c,'UserData',userData)), Children);
    TLSetCtrlsStatus(dlgfig, Children, ctrlNames);
else
    TLManageVariableCtrls('setCtrls', pageNum, dlgfig, dlgdata);
    TLManageScalingCtrls('setCtrls', pageNum, dlgfig, dlgdata);
end


%-----------------------------------------------------------------------------------------
% FcnCalcLoggingCtrlPositions calculates positions for all controls on logging page
%
% INPUT ARGUMENTS
%   commonGeom      common geometry information
%   sheetDims       dimension of sheet
%
% OUTPUT ARGUMENTS
%   sheetDims       modified dimension of sheet
%   ctrlPos         positions of controls
%-----------------------------------------------------------------------------------------
function [sheetDims, ctrlPos] = FcnCalcLoggingCtrlPositions(commonGeom, sheetDims)

excludedCtrls = {};
[sheetDims, ctrlPos] = ...
    TLManageLoggingCtrls('calcCtrls', commonGeom, sheetDims, excludedCtrls);

[selectLabelSize, selectPopupSize] = ds_get_uicontrol_sizes(commonGeom, ...
    {'Output to log:', 'text'; ...
    'fraction', 'popupmenu'});

widths = selectPopupSize(1);
[col,colWidth,colDelta,rowDelta] = ds_get_gui_columns(widths,commonGeom);

% enlarge logging groupbox
ctrlPos.logplotGroupbox(4) = ctrlPos.logplotGroupbox(4) ...
    + commonGeom.editHeight + rowDelta;

cy = ctrlPos.logLabel(2) + commonGeom.editHeight + rowDelta;
cx = ctrlPos.logLabel(1);
ctrlPos.selectLabel = [cx cy selectLabelSize];
cx = ctrlPos.logPopup(1);
ctrlPos.selectPopup = [cx cy selectPopupSize];

sheetDims(2) = commonGeom.sheetTopEdgeBuffer ...
    + ctrlPos.logplotGroupbox(4) ...
    + commonGeom.sheetBottomEdgeBuffer;


%-----------------------------------------------------------------------------------------
% FcnCreateLoggingCtrls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateLoggingCtrls(pageNum, dlgfig, dlgdata)

dlgdata = TLManageLoggingCtrls('create',pageNum,dlgfig,dlgdata);

Children = dlgdata.pageData{pageNum}.children;
ctrlPos = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};

% selection popup
Children.selectLabel = uicontrol( ...
    'Parent',             Children.logplotGroupbox, ...
    'Style',              'text', ...
    'String',             'Select:', ...
    'Position',           ctrlPos.selectLabel);

Children.selectPopup = uicontrol( ...
    'Parent',             Children.logplotGroupbox, ...
    'Style',              'Popupmenu', ...
    'BackgroundColor',    'w', ...
    'String',             {'index'; 'fraction'}, ...
    'Value',              1, ...
    'Position',           ctrlPos.selectPopup, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''selectPopup'')',pageName));

dlgdata.pageData{pageNum}.children = Children;

% set special values that are not overwritten by global function
dlgdata.extdata.logdata.bLimHroom = 1;
dlgdata.extdata.logdata.maxHroom  = Inf;


%-----------------------------------------------------------------------------------------
% FcnManageLoggingCtrls manage control actions of table page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata, bModified] = FcnManageLoggingCtrls(pageNum, dlgdata, action)

Children = dlgdata.pageData{pageNum}.children;

switch action
    case 'selectPopup'
        bModified = true;
        
    otherwise
        selectIdx = get(Children.selectPopup, 'value');
        vars = {'index', 'fraction'};
        logVars = {'logdata', 'flogdata'};
        [dlgdata, bModified] = ...
            TLManageLoggingCtrls('manage', pageNum, dlgdata, action, logVars{selectIdx}, vars{selectIdx});
end


%--------------------------------------------------------------------------
% FcnUpdateLoggingCtrls updates the controls of the documentation tab
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateLoggingCtrls(pageNum, dlgfig, dlgdata)

Children = dlgdata.pageData{pageNum}.children;

selectIdx = get(Children.selectPopup, 'value');
vars = {'index', 'fraction'};
logVars = {'logdata', 'flogdata'};
dlgdata =  TLManageLoggingCtrls('update',pageNum,dlgdata,logVars{selectIdx},vars{selectIdx});
TLManageLoggingCtrls('setCtrls',pageNum, dlgfig, dlgdata);


%-----------------------------------------------------------------------------------------
% FcnCreateDocumentationCtrls creates the figure window
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%-----------------------------------------------------------------------------------------
function dlgdata = FcnCreateDocumentationCtrls(pageNum, dlgfig, dlgdata)

% invoke generic function to create documentation page
dlgdata = TLManageDocCtrls('create',pageNum,dlgfig,dlgdata);

% create additional controls
Children = dlgdata.pageData{pageNum}.children;
ctrlPos = dlgdata.pageData{pageNum}.ctrlPos;
pageName = dlgdata.pageNames{pageNum};

Children.tblFcnName   = uicontrol( ...
    'Parent',            dlgfig, ...
    'Style',             'text', ...
    'String',            'Table function name: n.a.', ...
    'Position',          ctrlPos.tblFcnName);
Children.clipboardButton = uicontrol( ...
    'Parent',            dlgfig, ...
    'Style',             'pushbutton', ...
    'String',            'Copy to Clipboard', ...
    'Position',          ctrlPos.clipboardButton, ...
    'Callback',          sprintf('tldlg(''%s'',gcbf,''clipboardButton'')',pageName));

% save extended children handle structure
dlgdata.pageData{pageNum}.children = Children;


%-----------------------------------------------------------------------------------------
% FcnManageDocumentationCtrls manage control actions of ducumentation page
%
% INPUT ARGUMENTS
%   pageNum     number of current page
%   dlgfig      handle for dialog figure
%   dlgdata     dialog data
%   pageAction  action at one control of current page
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%   bModified   flag = 1, if dialog data are modified, =0 otherwise
%-----------------------------------------------------------------------------------------
function [dlgdata,bModified] = FcnManageDocumentationCtrls(pageNum, dlgfig, dlgdata, pageAction)

% default return value
bModified = 0;

% clipboard callbacks are handled directly here
if strcmp(pageAction, 'clipboardButton')
    clipboard('copy', dlgdata.pageData{pageNum}.tblFcnName);
    return
end

% invoke generic function to manage control callbacks
[dlgdata, bModified] = TLManageDocCtrls('callback', pageNum, dlgfig, ...
    dlgdata,pageAction);


%--------------------------------------------------------------------------
% FcnUpdateDocumentationCtrls updates the controls of the documentation tab
%
% INPUT ARGUMENTS
%   dlgdata     dialog data
%   dlgfig      figure handle of dialog
%   pageNum     page Number
%
% OUTPUT ARGUMENTS
%   dlgdata     modified dialog data
%--------------------------------------------------------------------------
function dlgdata = FcnUpdateDocumentationCtrls(pageNum, dlgfig, dlgdata)

TLManageDocCtrls('update', pageNum, dlgfig, dlgdata);

Children = dlgdata.pageData{pageNum}.children;
u = tl_eval_table(dlgdata.data.input, true);

tblFcnName = 'n.a.';
if ~isempty(u)
    % invoke get_plktabledata in batch mode
    b = ds_error_get('batchmode');
    ds_error_set('batchmode','on');
    [tableData,msgstruct] = get_plktabledata(dlgdata.data,u,inf);
    ds_error_set('batchmode',b);
    
    % determine tablefunction name
    if isempty(msgstruct) || ~any(strcmp({msgstruct.type}, 'Error'))
        tblFcnName = tl_get_tblfcnnames(dlgdata.data,dlgdata.model,tableData,dlgdata.maskType);
    end
    if iscell(tblFcnName), tblFcnName = [tblFcnName{:}]; end
end

tblFcnNameCtrlStatus = struct('enable', 'on');
clipboardButtonCtrlStatus = struct('enable', 'on');
tblFcnNameCtrlStatus.string = sprintf('Table function name: %s', tblFcnName);
clipboardButtonCtrlStatus.enable = flag2str(~isempty(tblFcnName) && ~strcmp(tblFcnName, 'n.a.'));
ctrlsList = {'tblFcnName', 'clipboardButton'};
for ctrlsNo=1:length(ctrlsList)
    ctrl = Children.(ctrlsList{ctrlsNo});
    UserData = get(ctrl, 'UserData');
    UserData.ctrlStatus = eval([ctrlsList{ctrlsNo} 'CtrlStatus']);
    set(ctrl, 'UserData', UserData);
end
TLSetCtrlsStatus(dlgfig, Children, ctrlsList);

% save table function name at dialog data structure
dlgdata.pageData{pageNum}.tblFcnName = tblFcnName;


%-----------------------------------------------------------------------------------------
% FcnbEnabled to get status for control
%
% INPUT ARGUMENTS
%   bWritable       is control write able
%   defaultStatus   is full version
%
% OUTPUT ARGUMENTS
%   enableStatus    status is 'on' or 'inactive'
%-----------------------------------------------------------------------------------------
function enableStatus = FcnbEnabled(bWritable, defaultStatus)

if defaultStatus && bWritable
    enableStatus = 'on';
else
    enableStatus = 'inactive';
end
